package com.wisdomstar.common.utils.file;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * @author RinMi
 * @since 2023/6/6
 */
public class MultipartFileUtil {

    public static void createFile(HttpServletResponse response, InputStream inputStream, String contentType, String filename){
        try {
            byte[] bytes = new byte[inputStream.available()];
            inputStream.read(bytes);
            response.setContentType(contentType);
            response.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
            response.setContentLength(bytes.length);
            response.setHeader("Content-Range", "" + (bytes.length - 1));
            response.setHeader("Accept-Ranges", "bytes");
            OutputStream os = response.getOutputStream();
            os.write(bytes);
            os.flush();
            os.close();
            inputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
